package Aleehot100;

import java.util.Stack;

/**
 * ClassName: c069
 * Package: Aleehot100
 * Description:20. 有效的括号
 *
 * @Author BCXJ
 * @Create 2025/8/17 09:07
 * @Version 1.0
 * @Since 1.0
 */
public class c069 {
    public static void main(String[] args) {
        String str = "]";
        System.out.println(isValid(str));
    }

    static Stack<Character> stack = new Stack<>();

    public static boolean isValid(String s) {
        stack.clear();
        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            if (!process(arr, i)) {
                return false;
            }
            ;
        }
        return stack.isEmpty();
    }

    private static boolean process(char[] arr, int i) {
        switch (arr[i]) {
            case '(':
                stack.push(arr[i]);
                return true;
            case '{':
                stack.push(arr[i]);
                return true;
            case '[':
                stack.push(arr[i]);
                return true;
        }
        switch (arr[i]) {
            case ')':
                if (!stack.isEmpty() && stack.pop() == '(') {
                    return true;
                } else {
                    return false;
                }
            case '}':
                if (!stack.isEmpty() && stack.pop() == '{') {
                    return true;
                } else {
                    return false;
                }
            case ']':
                if (!stack.isEmpty() && stack.pop() == '[') {
                    return true;
                } else {
                    return false;
                }
        }

        return false;
    }
}
